﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBUtility;//Please add references
namespace DAL
{
	/// <summary>
	/// 数据访问类:pe_ExamInfo
	/// </summary>
	public partial class pe_ExamInfo
	{
		public pe_ExamInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from pe_ExamInfo");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(Model.pe_ExamInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into pe_ExamInfo(");
			strSql.Append("ExamId,ExamName,ExamKind,CreateDT,BeginDT,EndDT,State,CreateMan,MonitorMan,paperBH,paperName,ExamBH,ExamTimeLen,csn,addDateTime)");
			strSql.Append(" values (");
			strSql.Append("@ExamId,@ExamName,@ExamKind,@CreateDT,@BeginDT,@EndDT,@State,@CreateMan,@MonitorMan,@paperBH,@paperName,@ExamBH,@ExamTimeLen,@csn,@addDateTime)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@ExamId", SqlDbType.Int,4),
					new SqlParameter("@ExamName", SqlDbType.VarChar,80),
					new SqlParameter("@ExamKind", SqlDbType.TinyInt,1),
					new SqlParameter("@CreateDT", SqlDbType.DateTime),
					new SqlParameter("@BeginDT", SqlDbType.DateTime),
					new SqlParameter("@EndDT", SqlDbType.DateTime),
					new SqlParameter("@State", SqlDbType.TinyInt,1),
					new SqlParameter("@CreateMan", SqlDbType.VarChar,20),
					new SqlParameter("@MonitorMan", SqlDbType.VarChar,20),
					new SqlParameter("@paperBH", SqlDbType.VarChar,30),
					new SqlParameter("@paperName", SqlDbType.VarChar,50),
					new SqlParameter("@ExamBH", SqlDbType.VarChar,20),
					new SqlParameter("@ExamTimeLen", SqlDbType.Int,4),
					new SqlParameter("@csn", SqlDbType.VarChar,50),
					new SqlParameter("@addDateTime", SqlDbType.DateTime)};
			parameters[0].Value = model.ExamId;
			parameters[1].Value = model.ExamName;
			parameters[2].Value = model.ExamKind;
			parameters[3].Value = model.CreateDT;
			parameters[4].Value = model.BeginDT;
			parameters[5].Value = model.EndDT;
			parameters[6].Value = model.State;
			parameters[7].Value = model.CreateMan;
			parameters[8].Value = model.MonitorMan;
			parameters[9].Value = model.paperBH;
			parameters[10].Value = model.paperName;
			parameters[11].Value = model.ExamBH;
			parameters[12].Value = model.ExamTimeLen;
			parameters[13].Value = model.csn;
			parameters[14].Value = model.addDateTime;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.pe_ExamInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update pe_ExamInfo set ");
			strSql.Append("ExamId=@ExamId,");
			strSql.Append("ExamName=@ExamName,");
			strSql.Append("ExamKind=@ExamKind,");
			strSql.Append("CreateDT=@CreateDT,");
			strSql.Append("BeginDT=@BeginDT,");
			strSql.Append("EndDT=@EndDT,");
			strSql.Append("State=@State,");
			strSql.Append("CreateMan=@CreateMan,");
			strSql.Append("MonitorMan=@MonitorMan,");
			strSql.Append("paperBH=@paperBH,");
			strSql.Append("paperName=@paperName,");
			strSql.Append("ExamBH=@ExamBH,");
			strSql.Append("ExamTimeLen=@ExamTimeLen,");
			strSql.Append("csn=@csn,");
			strSql.Append("addDateTime=@addDateTime");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@ExamId", SqlDbType.Int,4),
					new SqlParameter("@ExamName", SqlDbType.VarChar,80),
					new SqlParameter("@ExamKind", SqlDbType.TinyInt,1),
					new SqlParameter("@CreateDT", SqlDbType.DateTime),
					new SqlParameter("@BeginDT", SqlDbType.DateTime),
					new SqlParameter("@EndDT", SqlDbType.DateTime),
					new SqlParameter("@State", SqlDbType.TinyInt,1),
					new SqlParameter("@CreateMan", SqlDbType.VarChar,20),
					new SqlParameter("@MonitorMan", SqlDbType.VarChar,20),
					new SqlParameter("@paperBH", SqlDbType.VarChar,30),
					new SqlParameter("@paperName", SqlDbType.VarChar,50),
					new SqlParameter("@ExamBH", SqlDbType.VarChar,20),
					new SqlParameter("@ExamTimeLen", SqlDbType.Int,4),
					new SqlParameter("@csn", SqlDbType.VarChar,50),
					new SqlParameter("@addDateTime", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.BigInt,8)};
			parameters[0].Value = model.ExamId;
			parameters[1].Value = model.ExamName;
			parameters[2].Value = model.ExamKind;
			parameters[3].Value = model.CreateDT;
			parameters[4].Value = model.BeginDT;
			parameters[5].Value = model.EndDT;
			parameters[6].Value = model.State;
			parameters[7].Value = model.CreateMan;
			parameters[8].Value = model.MonitorMan;
			parameters[9].Value = model.paperBH;
			parameters[10].Value = model.paperName;
			parameters[11].Value = model.ExamBH;
			parameters[12].Value = model.ExamTimeLen;
			parameters[13].Value = model.csn;
			parameters[14].Value = model.addDateTime;
			parameters[15].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from pe_ExamInfo ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from pe_ExamInfo ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.pe_ExamInfo GetModel(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,ExamId,ExamName,ExamKind,CreateDT,BeginDT,EndDT,State,CreateMan,MonitorMan,paperBH,paperName,ExamBH,ExamTimeLen,csn,addDateTime from pe_ExamInfo ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
			parameters[0].Value = id;

			Model.pe_ExamInfo model=new Model.pe_ExamInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.pe_ExamInfo DataRowToModel(DataRow row)
		{
			Model.pe_ExamInfo model=new Model.pe_ExamInfo();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=long.Parse(row["id"].ToString());
				}
				if(row["ExamId"]!=null && row["ExamId"].ToString()!="")
				{
					model.ExamId=int.Parse(row["ExamId"].ToString());
				}
				if(row["ExamName"]!=null)
				{
					model.ExamName=row["ExamName"].ToString();
				}
				if(row["ExamKind"]!=null && row["ExamKind"].ToString()!="")
				{
					model.ExamKind=int.Parse(row["ExamKind"].ToString());
				}
				if(row["CreateDT"]!=null && row["CreateDT"].ToString()!="")
				{
					model.CreateDT=DateTime.Parse(row["CreateDT"].ToString());
				}
				if(row["BeginDT"]!=null && row["BeginDT"].ToString()!="")
				{
					model.BeginDT=DateTime.Parse(row["BeginDT"].ToString());
				}
				if(row["EndDT"]!=null && row["EndDT"].ToString()!="")
				{
					model.EndDT=DateTime.Parse(row["EndDT"].ToString());
				}
				if(row["State"]!=null && row["State"].ToString()!="")
				{
					model.State=int.Parse(row["State"].ToString());
				}
				if(row["CreateMan"]!=null)
				{
					model.CreateMan=row["CreateMan"].ToString();
				}
				if(row["MonitorMan"]!=null)
				{
					model.MonitorMan=row["MonitorMan"].ToString();
				}
				if(row["paperBH"]!=null)
				{
					model.paperBH=row["paperBH"].ToString();
				}
				if(row["paperName"]!=null)
				{
					model.paperName=row["paperName"].ToString();
				}
				if(row["ExamBH"]!=null)
				{
					model.ExamBH=row["ExamBH"].ToString();
				}
				if(row["ExamTimeLen"]!=null && row["ExamTimeLen"].ToString()!="")
				{
					model.ExamTimeLen=int.Parse(row["ExamTimeLen"].ToString());
				}
				if(row["csn"]!=null)
				{
					model.csn=row["csn"].ToString();
				}
				if(row["addDateTime"]!=null && row["addDateTime"].ToString()!="")
				{
					model.addDateTime=DateTime.Parse(row["addDateTime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,ExamId,ExamName,ExamKind,CreateDT,BeginDT,EndDT,State,CreateMan,MonitorMan,paperBH,paperName,ExamBH,ExamTimeLen,csn,addDateTime ");
			strSql.Append(" FROM pe_ExamInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,ExamId,ExamName,ExamKind,CreateDT,BeginDT,EndDT,State,CreateMan,MonitorMan,paperBH,paperName,ExamBH,ExamTimeLen,csn,addDateTime ");
			strSql.Append(" FROM pe_ExamInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM pe_ExamInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from pe_ExamInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "pe_ExamInfo";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

